val isEven: (Int) -> Boolean = { i: Int -> i % 2 == 0 }
val isOdd: (Int) -> Boolean = { it % 2 == 1 }
fun filterIt1Long(i: Int, func: (Int) -> Boolean): List<Int> =
buildList {
for (v in (0..i)) { // (0..i) is inclusive
if (func.invoke(v))
add(v)
}
}
fun filterIt1Short(i: Int, func: (Int) -> Boolean): List<Int> = (0..i).filter(func)
Function Call | Return Value | |||
---|---|---|---|---|
filterIt1Long(8, isEven) | → | |||
filterIt1Long(8, isOdd) | → | |||
filterIt1Short(6, isEven) | → | |||
filterIt1Short(6, isOdd) | → | |||
filterIt1Short(4) { i: Int -> i % 2 == 0 } | → | |||
filterIt1Short(4) { i: Int -> i % 2 != 0 } | → | |||
filterIt1Short(4) { it % 2 == 0 } | → | |||
filterIt1Short(4) { it % 2 != 0 } | → |
Experiment with this code on Gitpod.io or as a Kotlin Playground